package com.marvels.approve.manager.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.manager.dto.CaseManagerDTO;
import com.marvels.approve.manager.dto.CaseManagerVersionDTO;
import com.marvels.approve.manager.dto.ProcessFileDTO;
import com.marvels.approve.manager.service.IActivityLoadingService;
import com.marvels.approve.manager.service.ICaseManagerService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**管理流程定义
 * @author hejie
 */
@RestController
@RequestMapping("approve/manager/service")
public class CaseManagerServiceController {
	@Autowired
	private ICaseManagerService caseManagerService ;
	@Autowired
	private IActivityLoadingService activityLoadingService ;
	
	@RequestMapping("")
	@ResponseBody
	public MarvelsResultDTO approve(){
//		MarvelsResultDTO.getFailResult(MarvelsErrorCode.FAIL.getValue(), "调用服务失败，角色代码已经存在")
		CaseManagerDTO caseManager = null;
		try {
			caseManager = caseManagerService.findCaseManager(1L);
			MarvelsLogUtil.getInstance().info("init......"+caseManager);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	/**查询流程定义   code ,name ,status,pageNum,pageSize
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping("query")
	@ResponseBody
	public MarvelsResultDTO queryAll(HttpServletResponse response,@RequestBody(required=false) CaseManagerDTO caseManagerDto){
		List<CaseManagerDTO> cms = null;
		try {
//			cms = caseManagerService.findCaseManager(code, name, status, curPage, pageSize);
			cms =   caseManagerService.findCaseManager(caseManagerDto);
			MarvelsLogUtil.getInstance().info("init......"+cms);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("faile...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), "系统错误");
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), cms);
	}
	/**查询流程文件。文件ftp到服务器
	 * @return
	 */
	@Value("${bing.process.file.path}")
	private String processFilePath;
	@RequestMapping("queryfile")
	@ResponseBody
	public MarvelsResultDTO queryApprove(){
		String path = processFilePath+"/processes";
		List<ProcessFileDTO> pfs = new ArrayList();
		try {
			File dir = new File(path);
			File[] fs = dir.listFiles();
			MarvelsLogUtil.getInstance().info(path+" 文件数量："+fs);
			for(File f:fs){
				ProcessFileDTO dto = new ProcessFileDTO(f.getName(),f.getPath(),null);
				String fname = f.getName();
				if(fname==null||!fname.endsWith(".bpmn")) {
					continue;
				}
				pfs.add(dto);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), pfs);
	}
	/**创建流程定义
	 * @return
	 */
	@RequestMapping("create_process")
	@ResponseBody
	public MarvelsResultDTO createProcess(HttpServletResponse response,@RequestBody ProcessFileDTO fileDTO){
		try {
			activityLoadingService.loadProcessFile(fileDTO.getFilePath());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**查询流程版本
	 * @param response
	 * @param fileDTO
	 * @return
	 */
	@RequestMapping("create_process_version")
	@ResponseBody
	public MarvelsResultDTO queryProcessVersion(@RequestBody CaseManagerVersionDTO caseManagerVersionDto){
		try {
			List<CaseManagerVersionDTO> datas = this.caseManagerService.findProcessVersions(caseManagerVersionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("init...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
}
